// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.core.main;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.columba.core.resourceloader.GlobalResourceLoader;

/**
 * Parsing the commandline arguments and setting states, that can be used from
 * other components.
 *
 * @author waffel
 */
public class ColumbaCmdLineParser {

	private static final String RESOURCE_PATH = "org.columba.core.i18n.global";

	private CommandLineParser parser;

	private Options options;

	private String[] args;

	private static ColumbaCmdLineParser instance;

	private CommandLine commandLine;

	// switch for restoring last session of Columba.
	// if true, restores all windows.
	private boolean restoreLastSession = true;

	private ColumbaCmdLineParser() {
		parser = new BasicParser();
		options = new Options();
	}

	/**
	 * Gets the instance of the ColumbaCmdLineParser.
	 *
	 * @return the singleton instance
	 */
	public static ColumbaCmdLineParser getInstance() {
		if (instance == null) {
			instance = new ColumbaCmdLineParser();
		}

		return instance;
	}

	/**
	 * Adds an option to the CommandlineParser
	 *
	 * @param option
	 *            a new command line argument.
	 */
	public void addOption(Option option) {
		options.addOption(option);
	}

	/**
	 * Adds an OptionGroup to the CommandlineParser.
	 *
	 * @param option
	 */
	public void addOptionGroup(OptionGroup option) {
		options.addOptionGroup(option);
	}

	/**
	 * Parses the commandline.
	 *
	 * @param args
	 *            the arguments
	 * @return the parsed CommandLine
	 * @throws ParseException
	 */
	public CommandLine parse(String[] args) throws ParseException {
		commandLine = parser.parse(options, args);

		return commandLine;
	}

	/**
	 * Gets the previously parsed Commandline.
	 *
	 * @see #parse(String[])
	 *
	 * @return the last parsed commandline
	 */
	public CommandLine getParsedCommandLine() {
		return commandLine;
	}

	/**
	 * prints the usage of the program with commandline arguments.
	 */
	public void printUsage() {
		// automatically generate the help statement
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp(GlobalResourceLoader.getString(RESOURCE_PATH,
				"global", "cmdline_usage"), options);
	}

	/**
	 * @return Returns the args.
	 */
	public String[] getArgs() {
		return args;
	}

	/**
	 * @param restoreLastSession
	 *            The restoreLastSession to set.
	 */
	public void setRestoreLastSession(boolean restoreLastSession) {
		this.restoreLastSession = restoreLastSession;
	}

	public boolean getRestoreLastSession() {
		return this.restoreLastSession;
	}
}